package com.qf.fmcommodity.user.service.impl;

import com.alibaba.druid.sql.visitor.functions.If;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.fmcommodity.user.entity.ProductComments;
import com.qf.fmcommodity.user.entity.Users;
import com.qf.fmcommodity.user.mapper.ProductCommentsMapper;
import com.qf.fmcommodity.user.service.IProductCommentsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.fmcommodity.user.service.IUsersService;
import com.qf.fmcommodity.user.vo.Total;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品评价  服务实现类
 * </p>
 *
 * @author 苇名一心
 * @since 2023-02-13
 */
@Service
public class ProductCommentsServiceImpl extends ServiceImpl<ProductCommentsMapper, ProductComments> implements IProductCommentsService {

    @Autowired
    ProductCommentsMapper productCommentsMapper;

    @Autowired
    IUsersService iUsersService;

    @Override
    public Map<String, Object> detailCommonts(Integer id, Integer pageNum, Integer limit) {

        Page<ProductComments> page = new Page<>(pageNum,limit);


        page(page, new QueryWrapper<ProductComments>().eq("product_id", id));

        List<ProductComments> records = page.getRecords();
        int total = (int) page.getTotal();

        int current = (int) page.getPages();

        List<String> comments = records.stream().map(ProductComments::getUserId)
                .collect(Collectors.toList());

        QueryWrapper<Users> id1 = new QueryWrapper<Users>().in("user_id", comments);


        List<Users> list = iUsersService.list(id1);

        for (Users users : list) {
            for (ProductComments record : records) {
                if (users.getUserId() == Integer.parseInt(record.getUserId())) {
                    record.setUsername(users.getUsername());
                    record.setNickname(users.getNickname());
                    record.setUserImg(users.getUserImg());
                }
            }
        }


        Map<String,Object> map = new HashMap<>();

        map.put("pageCount",current);
        map.put("count",total);
        map.put("list",records);


        return map;
    }

    @Override
    public Total detailCommontscount(Integer id) {

        QueryWrapper<ProductComments> wrapper = new QueryWrapper<ProductComments>().eq("product_id", id);

        List<ProductComments> productCommentsList = list(wrapper);

        Total total = new Total();
        //好评
         Integer goodTotal = 0;
        //中评
         Integer midTotal = 0;
        //差评
         Integer badTotal = 0;

         double p = 0.0;

        for (ProductComments productComments : productCommentsList) {
            switch (productComments.getCommType()){
                case 1:
                     goodTotal++;
                    break;
                case 0:
                    midTotal++;
                    break;
                case -1:
                    badTotal++;
                    break;
            }
        }

        BigDecimal bigDecimal = new BigDecimal((double) goodTotal/productCommentsList.size()*100).setScale(2,BigDecimal.ROUND_HALF_UP);

        total.setTotal(productCommentsList.size());
        total.setGoodTotal(goodTotal);
        total.setMidTotal(midTotal);
        total.setBadTotal(badTotal);
        total.setPercent(bigDecimal.doubleValue());

        return total;

    }
}
